﻿namespace LowCoding.Common
{
    public static class ArgumentCheck
    {
        /// <summary>
        /// 参数检查
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void NotNull(object argument, string argumentName)
        {
            if (argument == null)
            {
                //ArgumentNullException 当调用方法并且至少一个传递的参数为null但绝不应该为空的时候,将引发异常(报错)
                //在运行时候引发异常,这两种情况都反映了开发人员的错误,
                //为实例化的对象讲传递给方法,若要防止次错误,请实例化对象.
                //然后,从方法调用返回的对象作为参数传递给第二个方法,但原始返回对象的为空值。
                //若要防止此错误,请检查返回值是否为空,并仅在返回值不为空时调用第二个方法
                throw new ArgumentNullException(argumentName);
            }
        }

        public static void GuidNotNull(Guid argument, string argumentName)
        {
            if (argument == Guid.Empty)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        public static void NotNullOrWhiteSpace(object argument, string argumentName)
        {
            if (argument.IsNullOrEmpty())
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        public static void NotNullOrWhiteSpace(string argument, string argumentName, Action<string> action)
        {
            if (string.IsNullOrWhiteSpace(argument))
            {
                action(argumentName);
            }
        }

        public static void NotLength(string argument, string argumentName, int length)
        {
            NotNullOrWhiteSpace(argument, argumentName);
            if (argument.Length != length)
            {
                throw new Exception($"字符串{argumentName}长度不为{length}");
            }
        }

        public static void NotZeroOrOne(string argument, string argumentName)
        {
            NotNullOrWhiteSpace(argument, argumentName);
            if (!ValidateHelper.IsZeroOrOne(argument))
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
        }

        public static void NotInt(string argument, string argumentName)
        {
            NotNullOrWhiteSpace(argument, argumentName);
            if (!argument.IsInt())
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
        }

        public static void NotDecimal(string argument, string argumentName)
        {
            if (!argument.IsDecimal())
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
        }

        public static void NotDate(string argument, string argumentName)
        {
            if (!argument.IsDate())
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
        }

        public static void NotBool(string argument, string argumentName)
        {
            if (!argument.IsBool())
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
        }

        public static void NotListModel<T>(string argument, string argumentName, out List<T> reslistmodel) where T : class, new()
        {
            NotNullOrWhiteSpace(argument, argumentName);
            if (!JsonSplit.IsJson(argument))
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
            List<T> resmodel = JsonHelper.FromJsonList<T>(argument);
            if (resmodel == null || resmodel.Count <= 0)
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
            reslistmodel = resmodel;
        }

        public static void NotEnum<T>(string argument, string argumentName)
        {
            if (!argument.IsInt())
            {
                throw new Exception($"传入参数{argumentName}有误");
            }
            NotNullOrWhiteSpace(Enum.GetName(typeof(T), Convert.ToInt32(argument)), argumentName);
        }

        public static void NotUint(string argument, string argumentName)
        {
            NotNullOrWhiteSpace(argument, argumentName);
            if (!argument.IsUint())
            {
                throw new Exception($"参数{argumentName}有误");
            }
        }
    }
}